/*
 * Copyright 2009 Andreas Borglin andreas.borglin@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package borge.mobile.app.jakoon;

import borge.mobile.lib.pimp.ImageConstants;
import borge.mobile.app.jakoon.Main.ESwtExecutor;
import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Slider;


/**
 *
 * @author Andreas Borglin
 */
public class ImageManipulatorForm implements ImageConstants {

    private Composite parent;
    private Composite top;
    private Composite settingsComp;
    private Composite imageComp;
    private Composite commandComp;
    private ESwtExecutor executor;
    // Needed
    private Slider brightnessSlider;
    private Slider contrastSlider;
    private Combo whitebalanceList;
    private Label imageLabel;
    private Label brightness;
    private Label contrast;

    private int curBrightness = 50;
    private int curContrast = 50;
    private int curWBIndex;
    private int opCount = 0;
    private int lastOpCount;
    private Vector opCounts;

    private ImageForm imageForm;

    private static final int NUM_COLUMNS = 1;

    protected ImageManipulatorForm(ESwtExecutor exec, Composite parent, ImageForm imgForm) {

        this.parent = parent;
        executor = exec;
        imageForm = imgForm;
        opCounts = new Vector();

        top = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 3;
        top.setLayout(layout);

        settingsComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.marginLeft = 15;
        layout.numColumns = NUM_COLUMNS;
        settingsComp.setLayout(layout);

        imageComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginLeft = 10;
        imageComp.setLayout(layout);

        commandComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        //layout.marginLeft = 50;
        layout.marginTop = 300;
        commandComp.setLayout(layout);



        // Title, centered and spawns over all columns
        Label title = new Label(settingsComp, SWT.NONE);
        title.setText("Image settings");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.CENTER;
        data.horizontalSpan = NUM_COLUMNS;
        data.heightHint = 40;
        title.setLayoutData(data);

        initSettingsArea();
        initImageArea();
        initCommandsArea();

        settingsComp.layout();
        imageComp.layout();
        commandComp.layout();
        top.layout();

    }

    protected void makeVisible(){
        top.setVisible(true);
    }

    protected void setWhitebalanceList(String[] list){

        whitebalanceList.removeAll();
        whitebalanceList.add("None");
        for(int i = 0; i < list.length; ++i){
            whitebalanceList.add(list[i]);
        }
        whitebalanceList.select(0);
    }

    protected void setImage(Image img){
        Rectangle imgRect = img.getBounds();
        Rectangle rect = CanvasUtil.scaledSize(imgRect.width, imgRect.height, 150, 150);
        Image newimg = new Image(executor.getDisplay(), img.getImageData().scaledTo(rect.width, rect.height));
        imageLabel.setImage(newimg);
    }

    private void initSettingsArea(){

        GridData data;

        brightness = new Label(settingsComp, SWT.WRAP);
        brightness.setText("Brightness\nChange: 0");

        brightnessSlider = new Slider(settingsComp, SWT.HORIZONTAL);
        brightnessSlider.setMinimum(0);
        brightnessSlider.setMaximum(100);
        brightnessSlider.setSelection(50);
        brightnessSlider.addSelectionListener(new SelectionListener(){
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                //save();
                Slider s = (Slider)event.getSource();
                brightness.setText("Brightness\nChange: " + (s.getSelection()-50));
                brightness.pack();
            }
        });

        contrast = new Label(settingsComp, SWT.WRAP);
        contrast.setText("Contrast\nChange: 0");

        contrastSlider = new Slider(settingsComp, SWT.HORIZONTAL);
        contrastSlider.setMinimum(0);
        contrastSlider.setMaximum(100);
        contrastSlider.setSelection(50);
        contrastSlider.addSelectionListener(new SelectionListener(){
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                //save();
                Slider s = (Slider)event.getSource();
                contrast.setText("Contrast\nChange: " + (s.getSelection()-50));
                contrast.pack();
            }
        });

        Label wb = new Label(settingsComp, SWT.NONE);
        wb.setText("WhiteBalance");

        whitebalanceList = new Combo(settingsComp, SWT.NONE);
        // add wbs..

    }

    private void initImageArea(){
        GridData data;

        imageLabel = new Label(imageComp, SWT.NONE);
        imageLabel.setText("Image");
        data = new GridData();
        data.horizontalSpan = 2;
        data.horizontalAlignment = GridData.BEGINNING;
        imageLabel.setLayoutData(data);

        Button preview = new Button(imageComp, SWT.PUSH);
        preview.setText("Preview");
        preview.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                //save();
                int countops = 0;
                if(brightnessSlider.getSelection() != curBrightness){
                    Data.commandMode = BRIGHTNESS;
                    imageForm.applyChange(brightnessSlider.getSelection());
                    countops++;
                    brightnessSlider.setSelection(curBrightness);
                    brightness.setText("Brightness\nChange: " +
                            (brightnessSlider.getSelection()-50));
                }

                if(contrastSlider.getSelection() != curContrast){
                    Data.commandMode = CONTRAST;
                    imageForm.applyChange(contrastSlider.getSelection());
                    countops++;
                    contrastSlider.setSelection(curContrast);
                    contrast.setText("Contrast\nChange: " +
                            (contrastSlider.getSelection()-50));
                }

                if(whitebalanceList.getSelectionIndex() != 0){
                    Data.commandMode = WHITEBALANCE;
                    imageForm.applyChange(whitebalanceList.getSelectionIndex()-1);
                    countops++;
                    whitebalanceList.select(0);
                }
                
                if(countops > 0){
                    opCounts.addElement(new Integer(countops));
                    setImage(imageForm.getCurrentImage());
                }

            }
        });

        Button undo = new Button(imageComp, SWT.PUSH);
        undo.setText("Undo");
        undo.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                //save();
                if(opCounts.size() > 0){
                    int ops = ((Integer)opCounts.elementAt(opCounts.size()-1)).intValue();
                    while(ops-- > 0)
                        imageForm.undo();
                    opCounts.removeElementAt(opCounts.size()-1);
                    
                    setImage(imageForm.getCurrentImage());
                }
            }
        });

    }
   
    private void initCommandsArea() {

        GridData data;


        Button save = new Button(commandComp, SWT.PUSH);
        save.setText("Save");
        save.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                //save();
                top.setVisible(false);
                executor.loadImageForm(false);
            }
        });
        data = new GridData();
        //data.widthHint = 70;
        //data.horizontalSpan = 3;
        //data.horizontalAlignment = GridData.END;
        save.setLayoutData(data);

        Button back = new Button(commandComp, SWT.PUSH);
        back.setText("Cancel");
        back.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {

                for(int i = 0; i < opCounts.size(); ++i){
                    int ops = ((Integer)opCounts.elementAt(i)).intValue();
                    for(int j = 0; j < ops; ++j)
                        imageForm.undo();
                }

                top.setVisible(false);
                executor.loadImageForm(false);
            }
        });
        data = new GridData();
        //data.widthHint = 70;
        //data.horizontalSpan = NUM_COLUMNS-1;
        //data.horizontalAlignment = GridData.BEGINNING;
        back.setLayoutData(data);
    }

}
